module executor.message.server.UnrecevieMsgExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import hunt.util.Serialize;
import client.MsgServerClient;
import client.ImClient;
import client.LogicServerClient;
import std.array;
import std.conv : to;
import core.stdc.time;

@TransportServer(gateServerName)
class UnrecevieMsgExecutor : AbstractExecutor!(UnrecevieMsgExecutor){

  @MessageId(MESSAGE.CMD_UNRECEIVE_REQ)
  void unrecevie(TransportContext ctx, MessageBuffer buffer) {
    logInfo("CMD_UNRECEIVE_REQ");
    auto req = new openReceivedMessageREQ();
    buffer.data.fromProtobuf!openReceivedMessageREQ(req);

    User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
    AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
    if (u !is null)
    {
      logInfo("CMD_UNRECEIVE_REQ ......");
      req.clientId = ImClient.getInstance().idMixSrc( to!ulong(u.id), authorize.src );
      LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_UNRECEIVE_REQ,req.toProtobuf.array));
    }
    else
    {
      logError("no authorized");
      //TODO
    }
  }

  @MessageId(MESSAGE.CMD_MESSAGE_ACK)
  void unrecevieACK(TransportContext ctx, MessageBuffer buffer) {
    auto req = new MsgAck();
    buffer.data.fromProtobuf!MsgAck(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_MESSAGE_ACK,req.toProtobuf.array));
  }

  @MessageId(MESSAGE.CMD_RED_SPOT)
  void redSpot(TransportContext ctx, MessageBuffer buffer) {
    auto req = new RED_SPOT();
    buffer.data.fromProtobuf!RED_SPOT(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_RED_SPOT,req.toProtobuf.array));
  }
}

